package com.xzc.datastruc.ujava.c07._1_introduction;

import javax.swing.*;
import java.awt.*;

public class DrawRuler extends JPanel {
    private int x = 200;
    private int y = 200;

    public void display() {
        x++;
        y++;
        this.repaint();
    }

    public void paint(Graphics g) {
        super.paint(g);
        Graphics2D g2d = (Graphics2D) g;
        drawRuler(g, 0, 100, 5);
    }

    void drawRuler(Graphics g, int left, int right, int level) {
        if (level < 1)
            return;
        int mid = (left + right) / 2;
        g.drawLine(mid, 80, mid, 80 - level * 5);
        drawRuler(g, left, mid - 1, level - 1);
        drawRuler(g, mid + 1, right, level - 1);
    }

    public static void main(String[] args) {
        JFrame jf = new JFrame();
        DrawRuler jp = new DrawRuler();

        jf.setBounds(200, 200, 500, 500);
        jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        jf.add(jp);
        jf.setVisible(true);

        while (true) {
            jp.display();

            try {
                Thread.sleep(300);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}
